package com.sonic.mquotes.report.core;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import com.sonic.log.ejb.impl.MQLogger;

/** 
 * MReport processor message consumer class.<br>
 * Supports the following gateway events:<br>
 *  - Client connected
 *  - Client disconnected
 *  - Client timeouted
 * 
 * @author <b>Adam Dec</b> for <b><I>iBroker</i></b><br> Copyright &#169; 2008/2009
 * @since 23-03-2009
 * @category BROKER PROCESSOR MDB
 * @version 1.0
 */
@MessageDriven(
		activationConfig = { 
				@ActivationConfigProperty(
						propertyName = "destinationType", 
						propertyValue = "javax.jms.Topic"
				),
				@ActivationConfigProperty(
						propertyName = "acknoweledgeMode", 
						propertyValue = "Auto-acknoweledge"
				)
		}, 
		mappedName = "iBusMobile_Events_GatewayEvents",
		name="MReportMessageProcessor",
		description="Consumes gateway events")
public class MReportMessageProcessor extends MReportMessageProcessorBase implements MessageListener {
	public static MQLogger logger = new MQLogger(MReportMessageProcessor.class, "MQ-REPORT-TOPIC");
	private static SimpleDateFormat formatter = new SimpleDateFormat("kk:mm:ss");
	
	@PostConstruct
	public void init() {
		final String methodName = "init()";
		logger.debug(methodName, " --- [INFO] Creating MReportMessageProcessor MDB...");
		
		logger.debug(methodName, " --- [INFO] MReportMessageProcessor MDB initialization complete.");
	}
	
	@PreDestroy
	public void destroy() {
		final String methodName = "destroy()";
		logger.debug(methodName, " --- [INFO] Destroying MReportMessageProcessor MDB...");
		
		logger.debug(methodName, " --- [INFO] MReportMessageProcessor MDB has been destroyed.");
	}
	
	public MReportMessageProcessor() { 
    	super();
    	final String methodName = "MReportMessageProcessor()";
    	logger.debug(methodName, " --- [INFO] Initializing MReportMessageProcessor class.");
	}
	
	public void onMessage(Message message) {
	    final String methodName = "onMessage()";
	    logger.debug(methodName, "New user appeared, trace: START");
	    
	    if(message != null && message instanceof MapMessage) {
	    	MapMessage mapMessage = (MapMessage)message;	    	
	    	try {
	        	String eventName   = mapMessage.getString(KEY_EVENT_NAME);
	        	long   createdTime = mapMessage.getLong(KEY_CREATED_TIME);
	    		final String eventCreationDate = formatter.format(new Date(createdTime));
	        	String gatewayName = mapMessage.getString(KEY_GATEWAY_NAME);
	    		
		    	if(eventName.compareTo(ClientConnectedEventName) == 0) {
		    		handleConnectedEvent(mapMessage);		    		
		    	} else if(eventName.compareTo(ClientDisconnectedEventName) == 0) {
		    		handleDisconnectedEvent(mapMessage);		    		
		    	} else if(eventName.compareTo(ClientTimeoutedEventName) == 0) {
		    		handleDisconnectedEvent(mapMessage);		    		
		    	} else if ( eventName.equals(GatewayStartedEventName) ) {
					handleGatewayStartedEvent(mapMessage); 
				} else if ( eventName.equals(GatewayStoppedEventName) ) {
					//TODO
				} else {
					logger.debug(methodName, "Unknown event:" + eventName );
				}	
	    	} catch(JMSException e) {
	    		logger.debug(methodName, "Could not get client parameters send by gateway, exception: " + e.getLocalizedMessage());
	    	}
	    }	    
	    logger.debug(methodName, "New user appeared, trace: END");	    
    }
	
	protected void showNumberOfActiveSessions() {
		final String methodName = "showNumberOfActiveSessions()";
		//logger.debug(methodName, "> Currently #" + activeSessions_.size() + " sessions are connected to iBus//Mobile");
    }
}